package com.fancy.projecty.serviceImpl;

import com.fancy.projecty.mapper.WaterAgentMapper;
import com.fancy.projecty.pojo.WaterAgent;
import com.fancy.projecty.service.WaterAgentService;
import com.fancy.projecty.util.ExceptionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class WaterAgentServiceImpl implements WaterAgentService {

	@Autowired
	private WaterAgentMapper waterAgentMapper;

	@Override
	public WaterAgent selectAgentByUserName(String username) {

		return this.waterAgentMapper.selectAgentByUserName(username);
	}

	@Override
	public WaterAgent selectById(Integer id) {
		WaterAgent waterAgent = this.waterAgentMapper.selectById(id);
		return waterAgent;
	}

	@Override
	public PageInfo<WaterAgent> selectByPage(Map map) {
		PageHelper.startPage(map);
		List<WaterAgent> waterAgentList = this.waterAgentMapper.selectAll(map);
		PageInfo<WaterAgent> waterAgentPageInfo = new PageInfo<>(waterAgentList);
		return waterAgentPageInfo;
	}

	@Override
	public void updateById(WaterAgent waterAgent) {
		int a =  this.waterAgentMapper.updateByPrimaryKeySelective(waterAgent);
		if (a == 0 ){
			throw new ExceptionUtil("修改失败");
		}
	}

	@Override
	public void insert(WaterAgent agent) {
		agent.setAgentCreatetime( new Date());
		this.waterAgentMapper.insertSelective(agent) ;
	}
}
